package com.example.leetdemo.finash;

public class Solution2047 {

    public static void main(String[] args) {
        Solution2047 solution2047 = new Solution2047();
        System.err.println(solution2047.countValidWords("-"));
    }


    public int countValidWords(String sentence) {
        String[] strs = sentence.split(" ");
        int count = 0;
        for (int i = 0 ; i < strs.length; i++) {
            if (!"".equals(strs[i]) && checkLine(strs[i]) && checkSymbol(strs[i])) {
                count++;
            }
        }
        return count;
    }

    private boolean checkLine(String str) {
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
                return false;
            }
            if (str.charAt(i) == '-') {
                count++;
                if (i == 0 || i == str.length() - 1 || count > 1) {
                    return false;
                }
                if (str.charAt(i - 1) < 'a' || str.charAt(i - 1) > 'z') {
                    return false;
                }
                if (str.charAt(i + 1) < 'a' || str.charAt(i + 1) > 'z') {
                    return false;
                }
            }
        }
        return true;
    }

    private boolean checkSymbol(String str) {
        for (int i = 0; i < str.length(); i++) {
            char w = str.charAt(i);
            if ((w == '.' || w == ',' || w == '!' ) && i != str.length() - 1) {
                return false;
            }
        }
        return true;
    }
}
